/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 24, 2012  4:18:20 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : SyncPrBudgetServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.promis.construction.SyncPrBudget;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class SyncPrBudgetServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(SyncPrBudgetServices.class);

	/**
	 * @param em
	 */
	public SyncPrBudgetServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public SyncPrBudget findById(String id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "SyncPrBudget.findById"));				
	}
	
	public List<SyncPrBudget> findAll() throws Exception {
		return super.ConvertToYoureObject(super.findAll("SyncPrBudget.findAll"));
	}
	
	public List<SyncPrBudget> findByPRId(String prHeaderId) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("prHeaderId", prHeaderId);
		
		return super.ConvertToYoureObject(super.find("SyncPrBudget.findByPRId", mapWhereValue));
	}
	
	public List<SyncPrBudget> findByRefVODocNumber(String company, String project, String refVoDocNumber, String status) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("refVoDocNumber", refVoDocNumber);
		mapWhereValue.put("status", status);
		
		return super.ConvertToYoureObject(super.find("SyncPrBudget.findByVODocument", mapWhereValue));
	}
	
	public SyncPrBudget findByPrDocument(String company, String project, String documentNo, int seqNo) throws Exception 
	{
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();		
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("documentNo", documentNo);
		mapWhereValue.put("seqNo", seqNo);
		
		List<SyncPrBudget> lst = super.ConvertToYoureObject(super.find("SyncPrBudget.findByPrDocument", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if(CommonService.getSize(lst) > 1)
			throw new Exception("Error has more then one [rec size=" + CommonService.getSize(lst) + "]");
		else
			return null;
	}

}
